/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "GetExpression3DDialog.h"
#include "ui_GetExpression3DDialog.h"
#include "Project.h"
#include "Expression3D.h"

GetExpression3DDialog::GetExpression3DDialog(Project *project, QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::GetExpression3DDialog)
{
    m_ui->setupUi(this);
    _project = project;
    if(_project){
        const QList<Expression3D*> & expressions = project->expressions();
        QStringList exprNames;
        for(int i = 0; i < expressions.count();++i){
            exprNames.append(expressions[i]->objectName());
        }
        m_ui->listWidget->addItems(exprNames);
    }
    connect(m_ui->listWidget,SIGNAL(currentTextChanged(QString)),this,SLOT(onCurrentTextChanged(QString)));
}

GetExpression3DDialog::~GetExpression3DDialog()
{
    delete m_ui;
}

void GetExpression3DDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void GetExpression3DDialog::onCurrentTextChanged(const QString& str){
    _expression = _project->findExpression(str);
    if(_expression){
        m_ui->plainTextEdit->clear();
        QString name = _expression->objectName();
        QString vars ="[";
        const QStringList & list = _expression->variablesList();
        for(int i = 0;i < list.count();++i){
            vars+=list[i];
            if(i<list.count()-1)
                vars+=",";
        }
        vars+="]";
        m_ui->plainTextEdit->appendPlainText(name+vars+"=< x"+vars+", y"+vars+", z"+vars+" >\n");
        m_ui->plainTextEdit->appendPlainText("x"+vars+"="+_expression->x());
        m_ui->plainTextEdit->appendPlainText("y"+vars+"="+_expression->y());
        m_ui->plainTextEdit->appendPlainText("z"+vars+"="+_expression->z());
    }
}

Expression3D* GetExpression3DDialog::expression(){
    return _expression;
}

QString GetExpression3DDialog::expressionName() const{
    if(_expression)
        return _expression->objectName();
    return QString();
}
